package com.midea.ai.air.ui.more.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.more.MoreSettingAirExpert;
import com.midea.ai.air.ui.more.MoreSettingBrainpower;
import com.midea.ai.air.ui.user.LoginActivity;
import com.midea.ai.air.view.CustomSwipeListView;
import com.midea.ai.air.view.SwipeItemView;

import java.util.List;

/**
 * Created by hww on 2016/3/29.
 */
public class MoreSettingBrainpowerAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Context context;
    private SwipeItemView mLastSlideViewWithStatusOn;
    private List<MoreSettingBrainpower.brainpower_context> list;

    public MoreSettingBrainpowerAdapter(Context context, List<MoreSettingBrainpower.brainpower_context> list) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        SwipeItemView slideView = (SwipeItemView) convertView;
        if (slideView == null) {
            View itemView = mInflater.inflate(R.layout.item_more_brainpower, null);
            slideView = new SwipeItemView(context);
            slideView.setContentView(itemView);
            holder = new ViewHolder(slideView);

            slideView.setOnSlideListener(new SwipeItemView.OnSlideListener() {
                @Override
                public void onSlide(View view, int status) {
                    if (mLastSlideViewWithStatusOn != null && mLastSlideViewWithStatusOn != view) {
                        mLastSlideViewWithStatusOn.shrink();
                    }
                    if (status == SLIDE_STATUS_ON) {
                        mLastSlideViewWithStatusOn = (SwipeItemView) view;
                    }
                }
            });

            slideView.setTag(holder);
        } else {
            holder = (ViewHolder) slideView.getTag();
        }

        if (CustomSwipeListView.mFocusedItemView != null) {
            CustomSwipeListView.mFocusedItemView.shrink();
        }

        MoreSettingBrainpower.brainpower_context bc = list.get(position);
        final int img_onoff = bc.getGetDrawable_on_off();

        holder.tv.setText(bc.getTv());
        holder.img_one.setImageResource(bc.getDrawable_one());
        holder.img_two.setImageResource(bc.getDrawable_two());
        holder.img_three.setImageResource(bc.getDrawable_three());
        holder.img_on_off.setImageResource(img_onoff);

        holder.isShow = bc.getState();
        holder.img_on_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.isShow) {
                    holder.img_on_off.setImageResource(R.drawable.more_setting_location_bt_off);
                    holder.isShow = false;
                } else {
                    holder.img_on_off.setImageResource(R.drawable.more_setting_location_bt_on);
                    holder.isShow = true;
                }
            }
        });


        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                LoginActivity.mLog("delete "+position);
                notifyDataSetChanged();
            }
        });

        return slideView;
    }


    class ViewHolder {
        private TextView tv;
        private ImageView img_one, img_two, img_three;
        private ImageView img_on_off;
        private boolean isShow;
        private ViewGroup delete;

        ViewHolder(View view) {
            tv = (TextView) view.findViewById(R.id.item_more_brainpower_tv);
            img_one = (ImageView) view.findViewById(R.id.item_more_brainpower_img_one);
            img_two = (ImageView) view.findViewById(R.id.item_more_brainpower_img_two);
            img_three = (ImageView) view.findViewById(R.id.item_more_brainpower_img_three);
            img_on_off = (ImageView) view.findViewById(R.id.item_more_brainpower_img_on_off);

            delete = (ViewGroup) view.findViewById(R.id.holder);
        }
    }
}
